package com.sdp.wx.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_labor_send.entity.WxLaborSend;
import com.sdp.wx.db.wx_labor_send.service.IWxLaborSendService;
import com.sdp.wx.db.wx_labor_send_sum.entity.WxLaborSendSum;
import com.sdp.wx.db.wx_labor_send_sum.service.IWxLaborSendSumService;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/WxLaborSendSumController")
public class WxLaborSendSumController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxLaborSendSumService laborSendSumService;
	
	@Autowired
	private IWxLaborSendService laborSendService;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(laborSendSumService, WxLaborSendSum.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxLaborSendSum laborSendSum) {
		if(laborSendSum == null) {
			return Result.error("提交信息为空");
		}
		if(StringUtils.isEmpty(laborSendSum.getKeyId())) {
			laborSendSum.setCreateTime(LocalDateTime.now());
			laborSendSumService.save(laborSendSum);
		} else {
			laborSendSumService.updateById(laborSendSum);
		}
		return Result.ok(laborSendSum);
	}
	
	@GetMapping(value = "/del")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		laborSendSumService.removeById(keyId);
		return Result.ok();
	}
	
	@PostMapping(value = "/init")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> init(@RequestBody WxLaborSend laborSend) {
		if(laborSend == null) {
			return Result.error("提交信息为空");
		}
		if (StringUtils.isEmpty(laborSend.getYearStr()) || StringUtils.isEmpty(laborSend.getQuarterStr())) {
			return Result.error("年或者季度信息为空");
		}
		
		List<WxLaborSendSum> resultList = laborSendService.laborSumList(laborSend.getYearStr(), laborSend.getQuarterStr());
		//先删除后添加
		QueryWrapper<WxLaborSendSum> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(WxLaborSendSum::getYearStr, laborSend.getYearStr())
			.eq(WxLaborSendSum::getQuarterStr, laborSend.getQuarterStr());
		laborSendSumService.remove(queryWrapper);
		
		laborSendSumService.saveBatch(resultList);
		
//		Map<String, WxLaborSendSum> resultMap = new HashMap<>();//结果
//		Map<String, Field> fieldMap = new HashMap<>();//反射设置劳保用品数量
//		Map<String, Map<String, String>> orgEmpNoMap = new HashMap<>();//单位人员map
//		Field[] fields = WxLaborSend.class.getDeclaredFields();
//		for (Field field : fields) {
//			fieldMap.put(field.getName(), field);
//		}
//		Field.setAccessible(fields, true);
//		QueryWrapper<WxLaborSend> queryWrapperSend = new QueryWrapper<>();
//		queryWrapperSend.lambda().eq(WxLaborSend::getYearStr, laborSend.getYearStr())
//			.eq(WxLaborSend::getQuarterStr, laborSend.getQuarterStr());
//		List<WxLaborSend> laborSendList = laborSendService.list(queryWrapperSend);
//		
//		try {
//			for (WxLaborSend wxLaborSend : laborSendList) {
//				if (!resultMap.containsKey(wxLaborSend.getOrgNo())) {
//					WxLaborSendSum laborSendSum = new WxLaborSendSum();
//					laborSendSum.setOrgNo(wxLaborSend.getOrgNo());
//					laborSendSum.setCreateTime(LocalDateTime.now());
//					laborSendSum.setYearStr(wxLaborSend.getYearStr());
//					laborSendSum.setQuarterStr(wxLaborSend.getQuarterStr());
//					laborSendSum.setEmpNum(BigDecimal.ZERO);
//					for (Field field : fields) {
//						if (field.getName().indexOf("item") >= 0) {
//							field.set(laborSendSum, BigDecimal.ZERO);
//						}
//					}
//					resultMap.put(wxLaborSend.getOrgNo(), laborSendSum);
//					orgEmpNoMap.put(wxLaborSend.getOrgNo(), new HashMap<>());
//				}
//				
//				if (!orgEmpNoMap.get(wxLaborSend.getOrgNo()).containsKey(wxLaborSend.getEmpNo())) {
//					orgEmpNoMap.get(wxLaborSend.getOrgNo()).put(wxLaborSend.getEmpNo(), wxLaborSend.getEmpNo());
//					resultMap.get(wxLaborSend.getOrgNo()).setEmpNum(resultMap.get(wxLaborSend.getOrgNo()).getEmpNum().add(BigDecimal.ONE));
//				}
//				for (Field field : fields) {
//					if (field.getName().indexOf("item") >= 0) {
//						if (field.get(wxLaborSend) != null) {
//							field.set(resultMap.get(wxLaborSend.getOrgNo()), ((BigDecimal) field.get(resultMap.get(wxLaborSend.getOrgNo()))).add(augend));
//						}
//						
//					}
//				}
//			}
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		}

		return Result.ok();
	}
}

